/*
 * main.c
 *
 *  Created on: Aug 26, 2014
 *      Author: root
 */
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "pthread.h"
#include "mlsMarlin.h"

static void cardAbsent(void *param)
{
	printf("CARD ABSENT\r\n\n\n");
	return;
}

static void cardPresent(void *param)
{
	printf("CARD PRSENT\r\n\n\n");
	return;
}

static const NfcHandlerFp _cbfc[] =
{		cardAbsent,
		cardPresent
};

void mlsPollCard(void)
{
	while (1)
	{
		mlsNfcUpdate(NULL);
		usleep(100000);
	}
}

void mlsGetCardStatus(void)
{
	UInt8 buffUID[50];
	UInt16 len = sizeof(buffUID) - 1;
	DiscoveryStates_t typeCard;
	UInt16 idx;

	while (1)
	{
		if(mlsNfcGetSlotStatus() == MLS_SUCCESS)
		{
			printf("Get card status: card present\n");
			mlsNfcGetCardSerialNumber(0,buffUID, &len);
			typeCard = mlsNfcGetCardType();
			switch(typeCard)
			{
			case STATE_TYPE_A:
				switch (mlsNfcGetTypeACardType())
				{
					case TYPEA_MF_ULTRALIGHT:
						printf("Card type A Ultralight\n");
						break;
					case TYPEA_MF_ULTRALIGHT_C:
						printf("Card type A Ultralight C\n");
						break;
					case TYPEA_MF_CLASSIC:
						printf("Card type A Classic\n");
						break;
					case TYPEA_MF_CLASSIC_1K:
						printf("Card type A Classic 1K\n");
						break;
					case TYPEA_MF_CLASSIC_4K:
						printf("Card type A Classic 4K\n");
						break;
					case TYPEA_MF_PLUS:
						printf("Card type A Plus\n");
						break;
					case TYPEA_MF_PLUS_2K_SL1:
						printf("Card type A Plus 2K SL1\n");
						break;
					case TYPEA_MF_PLUS_2K_SL2:
						printf("Card type A Plus 2K SL2\n");
						break;
					case TYPEA_MF_PLUS_2K_SL3:
						printf("Card type A Plus 2K SL3\n");
						break;
					case TYPEA_MF_PLUS_4K_SL1:
						printf("Card type A Plus 4K SL1\n");
						break;
					case TYPEA_MF_PLUS_4K_SL2:
						printf("Card type A Plus 4K SL2\n");
						break;
					case TYPEA_MF_PLUS_4K_SL3:
						printf("Card type A Plus 4K SL3\n");
						break;
					case TYPEA_MF_DESFIRE:
						printf("Card type A Desfire\n");
						break;
					case TYPEA_JCOP:
						printf("Card type Jcop\n");
						break;
					case TYPEA_MF_MINI:
						printf("Card type A Mini\n");
						break;
					case TYPEA_NPA:
						printf("Card type A NPA\n");
						break;
					case TYPEA_PART4:
						printf("Card type A Part 4\n");
						break;
					default:
						printf("Card type A Unknown\n");
						break;
				}
				break;

			case STATE_PAUSE_1:
				printf("Card type Pause 1\n");
				break;

			case STATE_TYPE_B:
				printf("Card type B\n");
				break;

			case STATE_TYPE_BP:
				printf("Card type BP\n");
				break;

			case STATE_TYPE_PICOPASS:
				printf("Card type Picopass\n");
				break;

			case STATE_TYPE_F212:
				printf("Card type Felica 212\n");
				break;

			case STATE_TYPE_F424:
				printf("Card type Felica 424\n");
				break;

			case STATE_PAUSE_2:
				printf("Card type Pause 2\n");
				break;

			case STATE_15693:
				printf("Card type ISO 15693\n");
				break;

			default:
				break;
			}

			printf("Card UID: ");
			for(idx =0; idx < len; idx ++)
			{
				printf("%02X ",buffUID[idx]);
			}
			printf("\n\n");
		}
		else
		{
			printf("Get card status: card absent\n");
		}
		sleep(1);
	}
}

int test_NFC()
{
	mlsNfcParam_t param;
	pthread_t poll, status;

	param.cardStatusChange = False;
	param.isTransparentMode = False;
	param._cbCardAbsent = _cbfc[0];
	param._cbCardPresent = _cbfc[1];

	mlsNfcInit(&param);

	pthread_create(&poll, NULL,(void*)&mlsPollCard, NULL);
	//pthread_create(&status, NULL, (void*)&mlsGetCardStatus, NULL);

	while(1)
	{
		sleep(10);
	}

	return 0;
}

int test_Board()
{

	return 0;
}

int main()
{
	test_NFC();
	return 0;
}

